﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.StockCountServiceRef;
    using WMS6.Web.UserControls;

    public class PCRequestList : BasePage
    {
        protected Button btnAction;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F5542;
        protected GridView gvPCRequest;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblMsg;
        protected Label lblReceiptInquiryList;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindDataPhysicalRequestList(PhysicalRequestQueryEntity physicalRequestQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(physicalRequestQueryEntity.CurrentPage);
            DataSet ds = new DataSet();
            ds = StockCountServiceClientProxy.GetInstance().GetPhysicalRequestDAByQueryEntity(physicalRequestQueryEntity);
            base.BindData2GridView(this.gvPCRequest, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvPCRequest, "PHYSICAL_REQUEST_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C009");
            }
            else
            {
                base.Response.Redirect(string.Format("PCRequest.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            PhysicalRequestQueryEntity RequestQueryEntity = this.GetPhysicalRequestQueryEntity();
            RequestQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = RequestQueryEntity.CurrentPage;
            RequestQueryEntity.CurrentPage = "0";
            this.gvPCRequest.Columns[1].Visible = false;
            this.gvPCRequest.AllowSorting = false;
            this.BindDataPhysicalRequestList(RequestQueryEntity);
            this.hdnActionID.Value = "";
            this.gvPCRequest.ShowFooter = false;
            RequestQueryEntity.IsGetAll = false;
            RequestQueryEntity.CurrentPage = currentPageIndex;
            this.SetPhysicalRequestQueryEntity(RequestQueryEntity);
            base.Export2Excel("PCRequest.xls", this.gvPCRequest);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            PhysicalRequestQueryEntity physicalRequest = this.GetPhysicalRequestQueryEntity();
            physicalRequest.CurrentPage = "0";
            this.BindDataPhysicalRequestList(physicalRequest);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            PhysicalRequestQueryEntity physicalRequest = this.GetPhysicalRequestQueryEntity();
            physicalRequest.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindDataPhysicalRequestList(physicalRequest);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            PhysicalRequestQueryEntity RequestQueryEntity = this.GetPhysicalRequestQueryEntity();
            RequestQueryEntity.PhysicalRequestID = "";
            RequestQueryEntity.Status = "";
            RequestQueryEntity.OwnerIdStart = "";
            RequestQueryEntity.OwnerIdEnd = "";
            RequestQueryEntity.SkuIdStart = "";
            RequestQueryEntity.SkuIdEnd = "";
            RequestQueryEntity.LocStart = "";
            RequestQueryEntity.LocEnd = "";
            RequestQueryEntity.LocLevelStart = "";
            RequestQueryEntity.LocLevelEnd = "";
            RequestQueryEntity.ZoneIdStart = "";
            RequestQueryEntity.ZoneIdEnd = "";
            RequestQueryEntity.AreaIdStart = "";
            RequestQueryEntity.AreaIdEnd = "";
            this.BindDataPhysicalRequestList(RequestQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            PhysicalRequestQueryEntity physicalRequest = this.GetPhysicalRequestQueryEntity();
            if (int.Parse(physicalRequest.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                physicalRequest.CurrentPage = (int.Parse(physicalRequest.CurrentPage) + 1).ToString();
                this.BindDataPhysicalRequestList(physicalRequest);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            PhysicalRequestQueryEntity physicalRequest = this.GetPhysicalRequestQueryEntity();
            if (int.Parse(physicalRequest.CurrentPage) > 0)
            {
                physicalRequest.CurrentPage = (int.Parse(physicalRequest.CurrentPage) - 1).ToString();
                this.BindDataPhysicalRequestList(physicalRequest);
            }
        }

        private PhysicalRequestQueryEntity GetPhysicalRequestQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as PhysicalRequestQueryEntity);
            }
            return (base.PageQueryEntitySession as PhysicalRequestQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                PhysicalRequestQueryEntity physicalRequest = this.GetPhysicalRequestQueryEntity();
                physicalRequest.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindDataPhysicalRequestList(physicalRequest);
            }
        }

        protected void gvPCRequest_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvPCRequest_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                PhysicalRequestQueryEntity physicalRequest = this.GetPhysicalRequestQueryEntity();
                physicalRequest.SortField = e.SortExpression;
                if (physicalRequest.SortDirection == "ASC")
                {
                    physicalRequest.SortDirection = "DESC";
                }
                else if (physicalRequest.SortDirection == "DESC")
                {
                    physicalRequest.SortDirection = "ASC";
                }
                this.BindDataPhysicalRequestList(physicalRequest);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            PhysicalRequestQueryEntity physicalRequest = new PhysicalRequestQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    physicalRequest.PhysicalRequestID = this.PreviousPage.PhysicalRequestID;
                    physicalRequest.Status = this.PreviousPage.Status;
                    physicalRequest.OwnerIdStart = this.PreviousPage.OwnerIDStart;
                    physicalRequest.OwnerIdEnd = this.PreviousPage.OwnerIDEnd;
                    physicalRequest.SkuIdStart = this.PreviousPage.SkuIDStart;
                    physicalRequest.SkuIdEnd = this.PreviousPage.SkuIDEnd;
                    physicalRequest.LocStart = this.PreviousPage.LocStart;
                    physicalRequest.LocEnd = this.PreviousPage.LocEnd;
                    physicalRequest.LocLevelStart = this.PreviousPage.LocLevelStart;
                    physicalRequest.LocLevelEnd = this.PreviousPage.LocLevelEnd;
                    physicalRequest.ZoneIdStart = this.PreviousPage.ZoneIDStart;
                    physicalRequest.ZoneIdEnd = this.PreviousPage.ZoneIDEnd;
                    physicalRequest.AreaIdStart = this.PreviousPage.AreaIDStart;
                    physicalRequest.AreaIdEnd = this.PreviousPage.AreaIDEnd;
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            physicalRequest.IsGetAll = false;
            physicalRequest.CurrentPage = "0";
            physicalRequest.PageSize = this.gvPCRequest.PageSize.ToString();
            physicalRequest.SortField = "PR.ROWID";
            physicalRequest.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvPCRequest.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetPhysicalRequestQueryEntity(physicalRequest);
            this.BindDataPhysicalRequestList(physicalRequest);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetPhysicalRequestQueryEntity(PhysicalRequestQueryEntity physicalRequestQueryEntity)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = physicalRequestQueryEntity;
            }
            else
            {
                base.PageQueryEntitySession = physicalRequestQueryEntity;
            }
        }

        public PCRequestQuery PreviousPage
        {
            get
            {
                return (PCRequestQuery) base.PreviousPage;
            }
        }
    }
}

